package com.deimos.sps;

import java.text.ParseException;
import java.util.Date;
import org.vast.ows.OWSException;
import org.vast.ows.sps.ReservationReport;
import org.vast.ows.sps.ReserveRequest;
import org.vast.ows.sps.ReserveResponse;
//import org.vast.ows.sps.StatusReport;
import org.vast.ows.sps.StatusReport.RequestStatus;
import org.vast.ows.sps.StatusReport.TaskStatus;
import org.vast.util.DateTime;
//import org.w3c.dom.Element;

import com.deimos.Notification;
import com.deimos.UtilsDeimos;
import com.deimos.dataBase.Event;
import com.deimos.dataBase.Sensor;
import com.deimos.dataBase.Task;
import com.deimos.dataBase.TaskController;

public class Reserve extends GetFeasibility {

    private ReserveResponse response;
    private ReserveRequest request;
    private DateTime reservationExpiration = null;
    //private TaskController taskController = new TaskController();
    //private String task = null;

    public Reserve(ReserveRequest request, Sensor sensor) throws ParseException, OWSException {
	super(request, null, Operation.RESERVED, sensor);

	this.request = (ReserveRequest) request;
	setExpirationTime();
	if (Reserve.isExpired(reservationExpiration)) {
	    System.out.println("RESERVE: The reservation date " + reservationExpiration.toString() + " is expired");
	    allowGetFeasibility = false;
	}

	/*task = taskController.isSensorInUse(request.getSensorID(), begin, end);
	if (task != null) {
	    allowGetFeasibility = false;
	}*/
    }

    public ReserveResponse getResponse() throws OWSException {
	response = (ReserveResponse) super.getResponse();
	//response.setReport(getReservationReport((StatusReport)response.getReport()));
	response.getReport().setReservationExpiration(reservationExpiration);
	if (!allowGetFeasibility) {
	    /*if (task != null)
		response.getReport().setDescription("The sensor " + response.getReport().getSensorID() + " is in use by the task " + task);
	    else*/
		response.getReport().setDescription("The expiration time is not a point in time in the future");
	    response.getReport().setRequestStatus(RequestStatus.Rejected);
	} else if (response.getReport().getRequestStatus().compareTo(RequestStatus.Accepted) == 0) {
	    response.getReport().setTaskStatus(TaskStatus.Reserved);
	} 
	return response;
    }

    public void addTask(ReserveResponse response) {

	TaskController taskController = new TaskController();
	Task task = new Task();
	Event event = new Event();

	ReservationReport report = response.getReport();
	task.setSensorId(request.getSensorID());
	task.setServiceType(response.getService());
	task.setTaskId(report.getTaskID());
	task.setTitle(report.getTitle());

	event.setOperation(Operation.RESERVED);
	event.setRequestStatus(report.getRequestStatus());
	event.setTaskStatus(report.getTaskStatus());
	event.setDescription(report.getDescription());
	event.setUpdateDate(new Date());
	event.setEstimatedToC(report.getEstimatedToC());
	event.setResponseParameters(UtilsDeimos.getParameters(response));
	event.setRequestParameters(UtilsDeimos.getParameters(request));
	event.setExpirationDate(report.getReservationExpiration());
	event.setFeasibilityStudy(report != null && report.getRequestStatus() == RequestStatus.Accepted);
	event.setBeginDate(begin);
	event.setEndDate(end);
	event.setMaxCloud(maxCloud);

	task.addEvent(event);
	task.setSegments(segments);

	taskController.insertTask(task);
	event.setTask(task);
	Notification.notifyReport(event);
    }

    public static boolean isExpired(Date date) {
	return date.before(new Date());
    }

    private void setExpirationTime() {
	// check max reservation
	DateTime maxReservation = new DateTime(new DateTime().getTime() + sensor.getMax_expiration());
	if (request.getReservationExpiration() == null || request.getReservationExpiration().after(maxReservation))
	    reservationExpiration = maxReservation;
	else
	    reservationExpiration = request.getReservationExpiration();
    }
    
    /*@SuppressWarnings("unchecked")
    private ReservationReport getReservationReport(StatusReport report){
	ReservationReport rreport = new ReservationReport();
	rreport.setDescription(report.getDescription());
	rreport.setEstimatedDelay((int) report.getEstimatedDelay());
	rreport.setEstimatedToC(report.getEstimatedToC());
	rreport.setEventCode(report.getEventCode());
	rreport.setLastUpdate(report.getLastUpdate());
	rreport.setPercentCompletion(report.getPercentCompletion());
	rreport.setRequestStatus(report.getRequestStatus());
	rreport.setSensorID(report.getSensorID());
	rreport.setStatusMessage(report.getStatusMessage());
	rreport.setTaskID(report.getTaskID());
	rreport.setTaskStatus(report.getTaskStatus());
	rreport.setTitle(report.getTitle());
	Iterator it = report.getExtensions().entrySet().iterator();
	while (it.hasNext()) {
	    Map.Entry e = (Map.Entry) it.next();
	    rreport.addExtension((Element) e.getValue());
	}
	return rreport;
    }*/
}
